#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

#include "securec.h"
#include "treeUtils.h"
int height(struct TreeNode *root)
{
    if (!root) {
        return 0;
    } else {
        return fmax(height(root->left), height(root->right)) + 1;
    }
}

bool isBalanced(struct TreeNode *root)
{
    if (!root) {
        return true;
    } else {
        return fabs(height(root->left) - height(root->right)) <= 1
        &&
        isBalanced(root->left) && isBalanced(root->right);
    }
}
int main()
{
    char str[] = "[3,9,20,null,null,15,7]";
    printf("%s\n", "start strtok string.");
    int count = 0;
    char retStrArr[100][10] = {0};
    getNum(str, retStrArr, &count);
    int numArr[9] = {0};
    for (int i = 0; i < count; ++i) {
        printf("%s\n", retStrArr[i]);
        numArr[i] = atoi(retStrArr[i]);
    }
    printf("%s\n", "end strtok string.");

    struct TreeNode *root = malloc(sizeof(struct TreeNode));
    root->val = numArr[0];
    root->left = NULL;
    root->right = NULL;
    bfsBuildTree(root, numArr, count);
    numArr[4] = 0;
    struct TreeNode *root1 = malloc(sizeof(struct TreeNode));
    root1->val = numArr[0];
    root1->left = NULL;
    root1->right = NULL;
    bfsBuildTree(root1, numArr, count);
    bool ret = isBalanced(root);
    printf("%s", ret ? "true" : "false");
}

